package com.styytech.dadabao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.bean.BankCard;

import java.util.LinkedList;

/**
 * 绑定银行卡适配器
 */
public class BankCardAdapter extends BaseAdapter {

    private LinkedList<Object> mList = null;
    private LayoutInflater mInflater = null;
    ViewHolder holder = null;
    private Context mContext;

    public BankCardAdapter(Context context, LinkedList<Object> mList) {
        this.mList = mList;
        this.mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        if(mList == null){
            return 0;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.item_bankcard, null);
            holder.iv_bankcard = (ImageView) convertView.findViewById(R.id.iv_bankcard);
            holder.tv_bankcard_name = (TextView) convertView.findViewById(R.id.tv_bankcard_name);
            holder.tv_bankcard_id = (TextView) convertView.findViewById(R.id.tv_bankcard_id);
            holder.v_line = convertView.findViewById(R.id.v_line);
            holder.v_spaceline = convertView.findViewById(R.id.v_spaceline);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BankCard itemData = (BankCard) mList.get(position);

        /*处理银行卡隐藏字符*/
        String accountNo = itemData.getAccountNo();
        String subAccountNo1 = accountNo.substring(3, accountNo.length() - 4);
        String subAccountNo = "";
        for (int i = 0; i < subAccountNo1.length(); i++) {
            subAccountNo += "*";
        }
        accountNo = accountNo.replace(subAccountNo1, subAccountNo);

        holder.tv_bankcard_name.setText(itemData.getBankName());
        holder.tv_bankcard_id.setText(accountNo);
        if(position == mList.size()-1){
            holder.v_line.setVisibility(View.GONE);
            holder.v_spaceline.setVisibility(View.VISIBLE);
        }else{
            holder.v_line.setVisibility(View.VISIBLE);
            holder.v_spaceline.setVisibility(View.GONE);
        }

        return convertView;
    }

    private class ViewHolder {
        //银行卡logo
        private ImageView iv_bankcard;
        //银行名
        private TextView tv_bankcard_name;
        //银行卡号
        private TextView tv_bankcard_id;

        private View v_line;
        private View v_spaceline;



    }
}
